package com.aurora.supermarket.base;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.Window;
import android.widget.Toast;

/**
 * author : ww on 2018/3/23.
 * time : 2017/08/18
 * fileNmae : AppBaseFragment
 * desc :
 */

public abstract class AppBaseFragment<T extends BasePresenter> extends Fragment implements BaseView{

    protected Context mActivity;
    public Boolean isViewCreated = false;  //View是否加载完成
    private Boolean isUIVisible = false;    //UI 用户是否可见
    public String memberId;
    protected T basePresenter;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        basePresenter = attach();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mActivity = context;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (basePresenter != null) {
            basePresenter.cancelRequest();
            basePresenter.detachView();
        }
        if(dialog!=null&&dialog.isShowing()){
            dialog.dismiss();
        }
    }

    public abstract T attach();

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        isViewCreated = true;

        lazyLoad();

    }

    private void lazyLoad() {
        //这里进行双重标记判断,是因为setUserVisibleHint会多次回调,并且会在onCreateView执行前回调,必须确保onCreateView加载完毕且页面可见,才加载数据
        if(isViewCreated&&isUIVisible){

            loadData();
            isUIVisible = false;
         //   isViewCreated = false;
        }

    }

    protected abstract void loadData();

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        if (isVisibleToUser) {
            isUIVisible = true;
            lazyLoad();
        } else {
            isUIVisible = false;
        }

    }

    private ProgressDialog dialog;

    public void showLoading() {
        if (dialog != null && dialog.isShowing()) return;
        dialog = new ProgressDialog(mActivity);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setMessage("正在加载中...");
        dialog.show();
    }

    public void dismissLoading() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    @Override
    public void showProcess(boolean show) {
        if(show){
            showLoading();
        }else{
            dismissLoading();
        }
    }

    @Override
    public void showMessage(String msg) {
        showShortToast(msg);
    }

    public void showLongToast(String message) {
        showToast(message, Toast.LENGTH_LONG);
    }

    public void showShortToast(String message) {
        showToast(message, Toast.LENGTH_SHORT);
    }

    private Toast mToast;
    private void showToast(String message, int duration) {
        if (mToast == null) {
            mToast = Toast.makeText(mActivity, message, duration);
        } else {
            mToast.setText(message);
            mToast.setDuration(Toast.LENGTH_SHORT);
        }
        mToast.show();
    }

}
